package com.ygxsk.carhome.svg;

import android.os.Looper;

import com.blankj.utilcode.util.PathUtils;
import com.blankj.utilcode.util.Utils;
import com.bumptech.glide.Glide;
import com.ygxsk.carhome.constant.ConstantSign;

import java.io.File;
import java.math.BigDecimal;


/**
 * Created by YaphetZhao
 * on 2016/12/19.
 * <p>
 * QQ:11613371
 * GitHub:https://github.com/YaphetZhao
 * Email:yaphetzhao@foxmail.com
 * Email_EN:yaphetzhao@gmail.com
 * <p>
 * Glide缓存工具类
 */
@SuppressWarnings("ResultOfMethodCallIgnored")
public class GlideCatchUtil {
    private static GlideCatchUtil instance;

    public static GlideCatchUtil getInstance() {
        if (null == instance) {
            instance = new GlideCatchUtil();
        }
        return instance;
    }

    // 获取Glide磁盘缓存大小
    public String getCacheSize() {

        /*String cachePath = new File(DingGovernApplication.getInstance().getCacheDir() + "/"
                + GlideCatchConfig.GLIDE_CARCH_DIR).getPath();*/

        //LogUtils.d("itchen--图片缓存路径="+cachePath);
        //itchen--图片缓存路径=/data/user/0/com.dgj.propertyowner/cache/image_catch
        //PropertyApplication.getInstance().getCacheDir()

        try {
            return getFormatSize(getFolderSize(new File(PathUtils.getInternalAppCachePath() + File.separator
                    + GlideCatchConfig.GLIDE_CARCH_DIR))
                    + getFolderSize(new File(Utils.getApp().getExternalCacheDir().getAbsolutePath() + File.separator + ConstantSign.DOWNAPP)));
        } catch (Exception e) {
            e.printStackTrace();
            return "获取失败";
        }
    }

    /*public String getApkCacheSize(){

        //String cachePath = new File(utils.getApp().getExternalCacheDir().getAbsolutePath()+ File.separator + "downapp").getPath();
        //LogUtils.d("itchen--apk缓存路径="+cachePath);
        //itchen--apk缓存路径=/storage/emulated/0/Android/data/com.dgj.propertyowner/cache/downapp
        try {
            return getFormatSize(getFolderSize(new File(utils.getApp().getExternalCacheDir().getAbsolutePath()+ File.separator + "downapp")));
        } catch (Exception e) {
            e.printStackTrace();
            return "获取失败";
        }

    }*/

    // 清除Glide磁盘缓存，自己获取缓存文件夹并删除方法
    public boolean cleanCatchDisk() {
        //return deleteFolderFile(PropertyApplication.getInstance().getCacheDir() + "/" + GlideCatchConfig.GLIDE_CARCH_DIR, true);
        return deleteFolderFile(PathUtils.getInternalAppCachePath() + File.separator + GlideCatchConfig.GLIDE_CARCH_DIR, true);
    }

    /**
     * 清除apk内容
     */
    public boolean cleanCacheApk() {

        //itchen----->文件=>/storage/emulated/0/Android/data/com.dgj.propertysmart/cache/downapp/社区管家21.apk<-----是否存在=>true


        //LogUtils.d("itchen-----清理缓存的APK地址-->"+Utils.getApp().getExternalCacheDir().getAbsolutePath()+ File.separator + ConstantApi.DOWNAPP);
        //itchen-----清理缓存的地址-->/storage/emulated/0/Android/data/com.dgj.propertysmart/cache/downapp
        //itchen-----清理缓存的地址-->/storage/emulated/0/Android/data/com.dgj.propertysmart/cache/downapp

        //return deleteFolderFile(Utils.getApp().getExternalCacheDir().getAbsolutePath() + File.separator + ConstantApi.DOWNAPP, true);
        return deleteFolderFile(PathUtils.getInternalAppCachePath() + File.separator + ConstantSign.DOWNAPP, true);
    }

    // 清除图片磁盘缓存，调用Glide自带方法
    public boolean clearCacheDiskSelf() {
        try {
            if (Looper.myLooper() == Looper.getMainLooper()) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        Glide.get(Utils.getApp()).clearDiskCache();
                    }
                }).start();
            } else {
                Glide.get(Utils.getApp()).clearDiskCache();
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    // 清除Glide内存缓存
    public boolean clearCacheMemory() {
        try {
            if (Looper.myLooper() == Looper.getMainLooper()) { //只能在主线程执行
                Glide.get(Utils.getApp()).clearMemory();
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }


    // 获取指定文件夹内所有文件大小的和
    private long getFolderSize(File file) throws Exception {
        long size = 0;
        try {
            File[] fileList = file.listFiles();
            for (File aFileList : fileList) {
                if (aFileList.isDirectory()) {
                    size = size + getFolderSize(aFileList);
                } else {
                    size = size + aFileList.length();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return size;
    }

    // 格式化单位
    //2018年03月06日的原始代码 正常显示byte kb等等
    /*private static String getFormatSize(double size) {
        double kiloByte = size / 1024;
        if (kiloByte < 1) {
            return size + "Byte";
        }
        double megaByte = kiloByte / 1024;
        if (megaByte < 1) {
            BigDecimal result1 = new BigDecimal(Double.toString(kiloByte));
            return result1.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "KB";
        }
        double gigaByte = megaByte / 1024;
        if (gigaByte < 1) {
            BigDecimal result2 = new BigDecimal(Double.toString(megaByte));
            return result2.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "MB";
        }
        double teraBytes = gigaByte / 1024;
        if (teraBytes < 1) {
            BigDecimal result3 = new BigDecimal(Double.toString(gigaByte));
            return result3.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "GB";
        }
        BigDecimal result4 = new BigDecimal(teraBytes);
        return result4.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "TB";
    }*/

    /**
     * 2018年03月06日itchen自己改造只显示MB的单位
     */
    public static String getFormatSize(double size) {

        //double kiloByte = size / 1024;
		/*if (kiloByte < 1) {//itchenqi修改2016年08月30日注释修改内容
			return size + "Byte";
		}*/

		/*double megaByte = kiloByte / 1024;//itchenqi修改2016年08月30日注释修改内容
		if (megaByte < 1) {
			BigDecimal result1 = new BigDecimal(Double.toString(kiloByte));
			return result1.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "KB";
		}*/

        //2016年08月30日修改需求要求只有MB单位###############################################################################
        double kiloByte = size / 1024;
        double megaByte = kiloByte / 1024;
        double gigaByte = megaByte / 1024;

        if (gigaByte < 1) {
            BigDecimal result2 = new BigDecimal(Double.toString(megaByte));
            return result2.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "MB";
        }
        //###############################################################################
        double teraBytes = gigaByte / 1024;
        if (teraBytes < 1) {
            BigDecimal result3 = new BigDecimal(Double.toString(gigaByte));
            return result3.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "GB";
        }
        BigDecimal result4 = new BigDecimal(teraBytes);
        return result4.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "TB";
    }

    // 按目录删除文件夹文件方法
    private boolean deleteFolderFile(String filePath, boolean deleteThisPath) {
        try {
            File file = new File(filePath);
            if (file.isDirectory()) {
                File[] files = file.listFiles();
                for (File file1 : files) {
                    deleteFolderFile(file1.getAbsolutePath(), true);
                }
            }
            if (deleteThisPath) {
                if (!file.isDirectory()) {
                    file.delete();
                } else {
                    if (file.listFiles().length == 0) {
                        file.delete();
                    }
                }
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

}
